package org.dk.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.dk.pojo.TbGoods;
import org.dk.pojo.TbGoodsDesc;
import org.dk.pojo.TbItem;
import org.dk.service.GoodsDescService;
import org.dk.service.GoodsService;
import org.dk.service.ItemService;
import org.dk.service.itempageservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class ItemPageServiceImpl implements itempageservice {

    @Autowired
    private FreeMarkerConfigurer freemarkerConfig;
    @Reference
    private GoodsService goodsService;
    @Reference
    private ItemService itemService;
    @Reference
    private GoodsDescService goodsDescService;

    @Override
    public void itempage(Long id) {
        //获取configration对象
        Configuration configuration = freemarkerConfig.getConfiguration();
        FileWriter writer = null;
        try {
            //创建模板对象 返回值
            Template template = configuration.getTemplate("text.ftl");
            TbGoods goods = goodsService.selectGoods(id);
            List<TbItem> tbItemList =  itemService.selectByGoodsId(id);
            TbGoodsDesc goodsDesc = goodsDescService.selectGoodsDesc(id);
            //创建map添加数据
            Map map = new HashMap();
            map.put("goods",goods);
            map.put("goodsDesc",goodsDesc);
            map.put("tbItemList",tbItemList);
            writer = new FileWriter("F:\\item_page\\" + id +".html");
            template.process(map,writer);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
                try {
                    if(writer != null){
                        writer.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}
